<!doctype html>

<html>

<head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  <title>Transport Test</title>
  <link rel="stylesheet" type="text/css" title="Default Style" href="../../style/button/new/style.css" media="screen" />

  <script type="text/javascript" basis-config="{}" src="../../src/basis.js"></script>
  <script type="text/javascript"  src="../../src/basis/data.js"></script>
  <script type="text/javascript"  src="../../src/basis/property.js"></script>
  <script type="text/javascript"  src="../../src/basis/crypt.js"></script>
  <script type="text/javascript"  src="../../src/basis/xml.js"></script>
  <script type="text/javascript"  src="../../src/basis/net/ajax_.js"></script>
  <script type="text/javascript"  src="../../src/basis/net/soap_.js"></script>
  <script type="text/javascript"  src="../../src/basis/dom_wrapper.js"></script>
  <script type="text/javascript"  src="../../src/basis/ui/button.js"></script>


  <style>
    #logOutput
    {
      width: 300px;
      height: 200px;
    }
  </style>
</head>

<body>
  <div id="container">
    <div><textarea id="logOutput"></textarea></div>
  </div>
  <script type="text/javascript">
    (function(){

      var DOM = basis.dom;
      var Event = basis.Event;

      var nsAjax = basis.net.ajax;
      var nsSoap = basis.net.soap;

      var DataObject = basis.data.DataObject;
      var Button = basis.ui.button.Button;
      var Transport = nsAjax.AjaxProxy;
      var SOAPTransport = nsSoap.SOAPProxy;

      var logOutput = DOM.get('logOutput')

      function clearLog(){
        logOutput.value = '';
      }
      function log(str){
        logOutput.value += str + '\n';
      }

      clearLog();

      var logCallback = {
        readyStateChanged: function(req, readyState)
        {
          log('readyState: ' + readyState);
        },
        start: function(){
          log('start event');
        },
        success: function(req){
          log('success event');
          log(req.data.responseText);

        },
        failure: function(){
          log('failure event');
        },
        complete: function(req){
          log('complete event');
          log('------------------');
        },
        abort: function(){
          log('abort event');
        },
        timeout: function(){
          log('timeout event');
        }
      };

      var influence1 = new DataObject({});
      influence1.addHandler({
        stateChanged: function(){
          log('influence1 state: ' + this.state);
        }
      });
      var influence2 = new DataObject({});
      influence2.addHandler({
        stateChanged: function(){
          log('influence2 state: ' + this.state);
        }
      });


      var transport = new Transport({
        url: 'result.json',
        method: 'GET',
        handler: logCallback
      });


      new Button({
        caption: 'Simple request',
        click: function(){
          clearLog();
          transport.get();
        },
        container: DOM.get('container')
      });

      new Button({
        caption: 'Simple request with timeout',
        click: function(){
          clearLog();
          transport.get({
            url: '/cgi-bin/delay.pl'
          });
        },
        container: DOM.get('container')
      });
     
      new Button({
        caption: 'Request with abort',
        click: function(){
          clearLog();
          transport.get({
            influence: influence1
          });
          transport.abort();
        },
        container: DOM.get('container')
      });

      new Button({
        caption: 'Two same request one by one',
        click: function(){
          clearLog();
          transport.get({
            influence: influence1
          });
          transport.get({
            influence: influence1
          });
        },
        container: DOM.get('container')
      });

      new Button({
        caption: 'Two diffenent request one by one',
        click: function(){
          clearLog();
            
          var transport = new Transport({
            url: 'result.json',
            method: 'get',
            handler: logCallback,
            poolHashGetter: function(data){
              return data.params.param.toFixed(2);
            },
            poolLimit: 1
          });
          
          transport.get({
            influence: influence1,
            params: { 'param': Math.random(1) }
          });

          transport.get({
            influence: influence2,
            params: { 'param': Math.random(1) }
          });

        },
        container: DOM.get('container')
      });

      var repeatCount = 0;
      new Button({
        caption: 'Transport with repeat',
        click: function(){
          clearLog();
          repeatCount = 0;
          var transport = new Transport({
            url: 'result.json',
            method: 'POST',
            handler: {
              complete: function(request){
                repeatCount++;

                if (repeatCount < 2)
                  request.repeat();
              }
            }
          });
          transport.get({
            influence: influence1
          });
        },
        container: DOM.get('container')
      });

      //SOAP

      var soapCallback = {
        start: function(){
          log('start event');
        },
        success: function(request){
          log('success event');              
        },
        complete: function(){
          log('complete event');
        },
        abort: function(){
          log('abort event');
        },
        failure: function(request, code, message){
          log('failure event');
          log(request.data.error.code + ': ' + request.data.error.message);
        },
        soapfailure: function(request, code, message){
          log('soapfailure event');
          log(code + ': ' + message);            
        }
      };

      var sessionKey = '243d59f6-5c88-44be-9023-180225657939';
      var namespace = 'Wallet.Security.WebService';

      var soapTransport = new SOAPTransport({
        url: '/w1service/SecurityService.asmx',
        namespace: namespace,
        methodName: 'GetSessionUserId',
        soapHeaderSections: {
          ParamsHeader: {
            namespace: namespace,
            data: {
              Params: {
                Param: [
                  {
                    '@Name':  'CultureId',
                    '@Value': 'ru-RU'
                  }
                ]
              }
            }
          }
        },
        soapBody: {
          SessionKey: sessionKey
        },
        handler: soapCallback,
        poolHashGetter: function(requestData){
          return requestData.soapBody.SessionKey;
        }
      });


      new Button({
        caption: 'SOAP Transport',
        click: function(){
          clearLog();
          soapTransport.get();
        },
        container: DOM.get('container')
      });
      new Button({
        caption: 'Two same SOAP request one by one',
        click: function(){
          clearLog();
          soapTransport.get();
          soapTransport.get();
        },
        container: DOM.get('container')
      });
      new Button({
        caption: 'Two different SOAP request one by one',
        click: function(){
          clearLog();
          soapTransport.get({
            soapBody: {  
              SessionKey: 'fe4c97c9-8e44-4314-9920-b33af86f1d33'              
            }
          });
          soapTransport.get({
            soapBody: {
              SessionKey: 'fe4c97c9-8e44-4314-9920-b33af86f1d34'              
            }
          });
        },
        container: DOM.get('container')
      });


      var customSOAPService = new nsAjax.Service({
        proxyClass: SOAPTransport,
        requestClass: nsSoap.SOAPRequest.subclass({
          responseDataGetter: function(data){
            var method = this.requestData.methodName;
            return data[method + 'Response'][method + 'Result'];
          }
        }),
        prepare: function(proxy){
          proxy.setSoapHeaderSection('ParamsHeader', {
            namespace: proxy.namespace,
            data: {
              Params: {
                Param: [
                  {
                    '@Name':  'CultureId',
                    '@Value': 'ru-RU'
                  }
                ]
              }
            }
          });

          return true;
        }
      });

      var customSOAPProxy = customSOAPService.createProxy({
        url: '/w1service/SecurityService.asmx',
        namespace: namespace,
        methodName: 'GetSessionUserId',
        soapBody: {
          SessionKey: sessionKey
        },
        handler: {
          success: function(request){
            var data = request.getResponseData();
            log(data);
          }
        }
      });


      new Button({
        caption: 'SOAP request via Custom Service',
        click: function(){
          clearLog();
          customSOAPProxy.get();
        },
        container: DOM.get('container')
      });


    })();
  </script>
</body>

</html>